<?php
	class School extends CI_Controller {
	   
       function __construct() {
	       parent::__construct();
           $user = $this->session->userdata('user');
           if (!$user) redirect('main/alogin');
           $this->load->model('school_db','School');
           $this->load->model('staff/staff_db','Staff');
           $this->load->model('Dataset_db');
	   }
       
       function addclass() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();

            if($_POST['btnAddClass']) {
                //print_object($_POST); exit;
                $pdata = array();
                $fields = array(
                		"class_label",
                        "class_agelimit"			
                		);
                foreach ($fields as $field) {
                	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                }
                
                $this->form_validation->set_rules('class_label', 'Class Label', 'trim|required|max_length[100]|xss_clean');
                $this->form_validation->set_rules('class_agelimit', 'Age Limit', 'trim|required|numeric|xss_clean');
                
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                
                
                if(!$this->School->is_class_unique($this->input->post("class_label",true))) {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Class already exist.");
                    else
                        $data["errors"] = array("Class already exist.");
                }
                
                $data["postdata"] = $pdata;
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    $user = $this->session->userdata('user');
                    $pdata["createdby"] = $user['userid'];
                    $pdata["createdon"] = unix_to_human(app_time(), true);
                    //print_object($pdata); exit;
                    
                    $save_result = $this->School->save_class($pdata);
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Class added successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            }
                                
            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $this->load->view('addclass',$data);
        }
        
        function classlist() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            $data['ptitle'] = "Class List";
            //print_object($_POST); exit;
            $filterdata = array(
                'items' => array(
                    array(
                        'label' => 'Class ID',
                        'table' => 'cl',
                        'name' => 'class_id',
                        'value' => isset( $_POST["class_id"] ) ? $_POST["class_id"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Class Label',
                        'table' => 'cl',
                        'name' => 'class_label',
                        'value' => isset( $_POST["class_label"] ) ? $_POST["class_label"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Age Limit',
                        'table' => 'cl',
                        'name' => 'class_agelimit',
                        'value' => isset( $_POST["class_agelimit"] ) ? $_POST["class_agelimit"] : '',
                        'type' => 'text',
                        'data' => ''
                    )
                ),
                'action' => '/transport/classlist',
                'submit' => 'btnSearchArea',
                'meta' => $this->School->get_class_meta(),
                'cols' => isset( $_POST["columns"] ) ? $_POST["columns"] : '',
            );
            
            if($this->input->post('show_all')) {
                $data['show_all'] = true;
            }
            
            $data['dgdata'] = $this->School->build_class_dg_data($filterdata);
            
            $data['filterdata'] = $filterdata;
               
            //print_object($data);exit;
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $this->load->view('searchitem',$data);            
        }
       
       
        function adddivision() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();

            if($_POST['btnAddDivision']) {
                //print_object($_POST); exit;
                $pdata = array();
                $fields = array(
                		"division_label",
                        "division_class_id",
                        "division_classteacher_id"			
                		);
                foreach ($fields as $field) {
                	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                }
                
                $this->form_validation->set_rules('division_class_id', 'Class', 'trim|required|xss_clean');
                $this->form_validation->set_rules('division_label', 'Division Label', 'trim|required|max_length[10]|xss_clean');
                $this->form_validation->set_rules('division_classteacher_id', 'Class Teacher', 'xss_clean');
               
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                
                
                if(!$this->School->is_division_unique($this->input->post("division_class_id",true), $this->input->post("division_label",true))) {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Division already exist.");
                    else
                        $data["errors"] = array("Division already exist.");
                }
                $data["postdata"] = $pdata;
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    $user = $this->session->userdata('user');
                    $pdata["createdby"] = $user['userid'];
                    $pdata["createdon"] = unix_to_human(app_time(), true);
                    //print_object($pdata); exit;
                    
                    $save_result = $this->School->save_division($pdata);
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Division added successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            }
                                
            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['classlist'] = $this->School->get_class_list();
            $data['teacherlist'] = $this->Staff->get_stafflist(1);
            $this->load->view('adddivision',$data);
        }
        
        function divisionlist() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            $data['ptitle'] = "Division List";
            //print_object($_POST); exit;
            $filterdata = array(
                'items' => array(
                    array(
                        'label' => 'Division ID',
                        'table' => 'dv',
                        'name' => 'division_id',
                        'value' => isset( $_POST["division_id"] ) ? $_POST["division_id"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Division Label',
                        'table' => 'dv',
                        'name' => 'division_label',
                        'value' => isset( $_POST["division_label"] ) ? $_POST["division_label"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Class',
                        'table' => 'dv',
                        'name' => 'division_class_id',
                        'value' => isset( $_POST["division_class_id"] ) ? $_POST["division_class_id"] : '',
                        'type' => 'select',
                        'data' => format_list($this->School->get_class_list(),"class_id", "class_label")
                    ),
                    array(
                        'label' => 'Class Teacher',
                        'table' => 'dv',
                        'name' => 'division_classteacher_id',
                        'value' => isset( $_POST["division_classteacher_id"] ) ? $_POST["division_classteacher_id"] : '',
                        'type' => 'select',
                        'data' => format_list($this->Staff->get_stafflist(1),'staff_id','staff_name')
                    )
                ),
                'action' => '/school/divisionlist',
                'submit' => 'btnSearchDivision',
                'meta' => $this->School->get_division_meta(),
                'cols' => isset( $_POST["columns"] ) ? $_POST["columns"] : '',
            );
            
            if($this->input->post('show_all')) {
                $data['show_all'] = true;
            }
            
            $data['dgdata'] = $this->School->build_division_dg_data($filterdata);
            
            $data['filterdata'] = $filterdata;
               
            //print_object($data);exit;
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $this->load->view('searchitem',$data);
        }
        
        function addterm() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; 
            }
            //print_object($_POST); exit;
            $data = array();

            if($_POST['btnAddTerm']) {
                //print_object($_POST); exit;
                $pdata = array();
                $fields = array(
                		"term_name",
                        "term_start",
                        "term_end"			
                		);
                foreach ($fields as $field) {
                	if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
                }
                
                $this->form_validation->set_rules('term_name', 'Term Name', 'trim|required|min_length[2]|max_length[50]|xss_clean');
                $this->form_validation->set_rules('term_start', 'Term Start', 'trim|required|xss_clean');
                $this->form_validation->set_rules('term_end', 'Term End', 'trim|required|xss_clean');
               
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                
                
                if(!$this->School->is_term_unique($this->input->post("term_name",true))) {
                    if(is_array($data['errors']))
                        array_push($data["errors"],"Term already defined.");
                    else
                        $data["errors"] = array("Term already defined.");
                }
                $data["postdata"] = $pdata;
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    $user = $this->session->userdata('user');
                    $pdata["createdby"] = $user['userid'];
                    $pdata["createdon"] = unix_to_human(app_time(), true);
                    //print_object($pdata); exit;
                    
                    $save_result = $this->School->save_term($pdata);
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Term added successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            }
                                
            $navResult = $this->Dataset_db->buildNav();
            //print_object($navResult); exit;
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $this->load->view('addterm',$data);
        }
        
        function editschool() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
               echo "You do not have permission to view this page"; exit; 
            }
            
            $data = array();
            $data['ptitle'] = "Edit School";
            if($_POST['btnUpdateUser']) {
                //print_object($_POST); exit;
                $pdata = array();
                $fields = array(
						"name"
                        ,"roleid"
						,"disabled"
                        ,"mobile"
                        ,"email"
                        ,"address"				
						);
                foreach ($fields as $field) {
    				if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field, true);
    			}
                
                $this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[3]|max_length[100]|xss_clean');
                $this->form_validation->set_rules('roleid', 'Account Type', 'trim|required|integer|xss_clean');
                $this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|min_length[5]|max_length[15]|xss_clean');
                $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|min_length[5]|max_length[50]|xss_clean');
                $this->form_validation->set_rules('address', 'Address', 'trim|max_length[200]|xss_clean');
                $this->form_validation->run();
                $data["errors"] = $this->form_validation->error_array();
                
                $data["postdata"] = $pdata;
                   
                if (count($data["errors"])>0) {
                    
                } else {
                    
                    $save_result = $this->Users->save_user($pdata,$this->input->post('userid',true));
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('User updated successfully.');
                        $data["postdata"] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            }
            
            if( isset( $_POST["userid"] ) ) {
                $itemid = xss_clean($itemid);
                $mi_result = $this->Users->get_user(array('userid'=> $this->input->post("userid",true)));
                if(count($mi_result) == 0) {
                    $data['errors'] = "Could not find the selected user";
                } else {
                    $data['userid'] = $mi_result[0]['userid'];
                    $data['username'] = $mi_result[0]['username'];
                    $data['roleid'] = $mi_result[0]['roleid'];
                    $data['disabled'] = $mi_result[0]['disabled'];
                    $data['name'] = $mi_result[0]['name'];
                    $data['mobile'] = $mi_result[0]['mobile'];
                    $data['email'] = $mi_result[0]['email'];
                    $data['address'] = $mi_result[0]['address'];
                    $data['roletitle'] = $mi_result[0]['roletitle'];
                }
            }
            
            //print_object($data); exit;
            $data["school"] = $this->School->get_school_info();                        
            $navResult = $this->Dataset_db->buildNav();
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['countrylist'] = $this->Dataset_db->get_countries();
            $this->load->view('editschool',$data);    
        }
    }       
/* End of file */